package com.intelligent.config;

import com.intelligent.util.RedisManagementUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class RedisStartupListener {

    private static final Logger logger = LoggerFactory.getLogger(RedisStartupListener.class);

    @Autowired
    private RedisManagementUtils redisManagementUtils;

    /**
     * 应用启动完成后检查Redis状态
     */
    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady() {
        logger.info("Application ready, checking Redis status...");
        
        try {
            // 检查并修复Redis状态
            redisManagementUtils.checkAndFixRedisStatus();
            
            // 测试Redis写操作
            boolean writeTest = redisManagementUtils.testRedisWrite();
            if (writeTest) {
                logger.info("Redis is ready for write operations");
            } else {
                logger.warn("Redis write test failed, there may still be issues");
            }
        } catch (Exception e) {
            logger.error("Error during Redis startup check: ", e);
        }
    }
}